package com.controller.wmt;

import com.entity.Building;

import com.entity.HouseDetail;
import com.entity.Room;
import com.entity.WmtBuildingList;
import com.service.wmt.BuildManagerService;


import com.service.wmt.BuildingService;
import com.service.wmt.RoomService;
import com.util.gzp.AjaxResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("building")
public class BuildingController {

    @Resource
    private BuildingService buildingService;

    @Resource
    private BuildManagerService buildManagerService;

    @Resource
    private RoomService roomService;



    /**
     * 根据楼盘ID查询楼盘所属
     * @param hid
     * @return
     */
    @RequestMapping("listAll")
    @ResponseBody
    public AjaxResponse listAll(Integer hid){
        List<Building> buildingList = buildingService.findAllBuilding(hid);
        AjaxResponse ajaxResponse = AjaxResponse.success(buildingList);
        return ajaxResponse;
    }

    /**
     * 链接查询结果,分页
     * @param hid
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public AjaxResponse list(Integer hid,Integer pageNum, Integer pageSize){
        System.out.println("BuildingController.list"+(buildingService.findAll(hid,pageNum,pageSize)).getData());
        return AjaxResponse.success(buildingService.findAll(hid,pageNum,pageSize));
    }

    @RequestMapping("addBuilding")
    @ResponseBody
    public  AjaxResponse addBuilding(Building building){
        System.out.println("BuildingController.addBuilding"+building);
        Integer integer = buildingService.insert(building);
        System.out.println("成功插入了吗"+integer);
        Building building1 = buildingService.findLastInsert();
        System.out.println("BuildingController.addBuilding"+building1);
        Integer floor = building.getFloor();
        Integer count = building.getCount();
        for (int i=1; i<=floor;i++){
            for (int j=1; j<=count ;j++){
                Room room = new Room();
                room.setRoomnumber(building.getBuildingnumber()+i+""+"0"+j+"");
                room.setRoomstatusid(2);
                room.setBuildingid(building.getBuildingid());
                System.out.println("BuildingController.addBuilding 插入的room对象为+"+room);
                roomService.insertRoom(room);
            }
        }
        return AjaxResponse.success(building1);
    }


    @RequestMapping("findBuildingByID")
    @ResponseBody
    public AjaxResponse findBuildingByID(Integer buildingid){
      Building building=buildingService.findByBuildingID(buildingid);


        AjaxResponse ajaxResponse = AjaxResponse.success(building);
        return ajaxResponse;
    }
    @RequestMapping("findBuildingByBuildingIdAndHid")
    @ResponseBody
    public AjaxResponse findBuildingByID(Integer buildingid,Integer hid){
        AjaxResponse ajaxResponse = AjaxResponse.success(buildingService.findByBuildingID(buildingid));
        return ajaxResponse;
    }


}
